﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GestionSaludOcupacional.Infrastructure.Mapping;
using GestionSaludOcupacional.Infrastructure.Services;
using GestionSaludOcupacional.Portal.Base.ViewModels.Examenes;
using GestionSaludOcupacional.Portal.App_Start;

namespace GestionSaludOcupacional.Portal.Controllers
{
    public class ExamenController : Controller
    {
        //
        // GET: /Examen/

        [Import]
        public IHealthService HealthService { get; set; }

        [Import]
        public IMappingService MappingService { get; set; }

        [Authorize]
        public ActionResult Index()
        {
            return View();
        }

        [Authorize]
        public ActionResult Vision(int id, int? citaId, int? key)
        {

            var schema = new ExamenVisionViewModel
            {
                IndividuoId = id,
                IdCita = citaId.HasValue ? citaId.Value : 0
            };

            if (key.HasValue)
            {
                var result = HealthService.ObtenerExamenVision(key.Value);
                schema = MappingService.GetViewModel(result);
            }

            ViewBag.Model = schema;
            return View();
        }

        [Authorize]
        public ActionResult Psicosensometrico(int id, int? citaId, int? key)
        {
            var schema = new ExamenPsicosensometriaViewModel
            {
                IndividuoId = id,
                IdCita = citaId.HasValue ? citaId.Value : 0
            };

            if (key.HasValue)
            {
                var result = HealthService.ObtenerExamenPsicosensometria(key.Value);
                schema = MappingService.GetViewModel(result);
            }
            ViewBag.Model = schema;
            return View();
        }

        [Authorize]
        public ActionResult Audicion(int id, int? citaId, int? key)
        {
            var schema = new ExamenAudicionViewModel
            {
                IndividuoId = id,
                IdCita = citaId.HasValue ? citaId.Value : 0

            };
            if (key.HasValue)
            {
                var result = HealthService.ObtenerExamenAudicion(key.Value);
                schema = MappingService.GetViewModel(result);
            }
            ViewBag.Model = schema;
            return View();
        }

        [Authorize]
        public ActionResult Psicologia(int id, int? citaId, int? key)
        {
            var schema = new ExamenPsicologiaViewModel
                             {
                                 IndividuoId = id,
                                 IdCita = citaId.HasValue ? citaId.Value : 0
                             };
            if (key.HasValue)
            {
                var result = HealthService.ObtenerExamenPsicologia(key.Value);
                schema = MappingService.GetViewModel(result);
            }
            ViewBag.Model = schema;
            return View();
        }

        public JsonResult GuardarExamenVision(ExamenVisionViewModel examenVision)
        {
            if (ModelState.IsValid)
            {
                var isValid = HealthService.GuardarExamenVision(MappingService.GetModel(examenVision));
                return Json(new { isValid });
            }
            return Json(new { isValid = false, message = ModelState.GetErrors() }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GuardarExamenPsicosensometrico(ExamenPsicosensometriaViewModel examenPsicosensometria)
        {
            if (ModelState.IsValid)
            {
                var model = MappingService.GetModel(examenPsicosensometria);
                var isValid = HealthService.GuardarExamenPsicosensometria(model);
                return Json(new { isValid });
            }
            return Json(new { isValid = false, message = ModelState.GetErrors() }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GuardarExamenAudicion(ExamenAudicionViewModel examenAudicion)
        {
            if (ModelState.IsValid)
            {
                var model = MappingService.GetModel(examenAudicion);
                var isValid = HealthService.GuardarExamenAudicion(model);
                return Json(new { isValid });
            }
            return Json(new { isValid = false, message = ModelState.GetErrors() }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GuardarExamenPsicologia(ExamenPsicologiaViewModel examenPsicologia)
        {
            if (ModelState.IsValid)
            {
                var model = MappingService.GetModel(examenPsicologia);
                var isValid = HealthService.GuardarExamenPsicologia(model);
                return Json(new { isValid });
            }
            return Json(new { isValid = false, message = ModelState.GetErrors() }, JsonRequestBehavior.AllowGet);
        }
    }
}